package com.log4jviewer.ui.preferences.filters;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.swt.SWT;

import com.log4jviewer.filters.FilterModel;

/**
 * Create possibility choose the filter in Filters Table
 * 
 * @author Daniil Lopatin
 */
public class FilterChoiceCellEditor extends EditingSupport {

    private final FilterSettings filterSettings;

    public FilterChoiceCellEditor(final FilterSettings filterSettings) {
        super(filterSettings.getTableViewer());
        this.filterSettings = filterSettings;
    }

    @Override
    protected CellEditor getCellEditor(final Object element) {
        return new CheckboxCellEditor(filterSettings.getTableViewer().getTable(), SWT.CHECK | SWT.CENTER);
    }

    @Override
    protected boolean canEdit(final Object element) {
        return true;
    }

    @Override
    protected Object getValue(final Object element) {
        return filterSettings.isFilterActive((FilterModel) element);
    }

    @Override
    protected void setValue(final Object element, final Object value) {
        if ((Boolean) value) {
            int activeFilterIndex = filterSettings.getFilterIndex((FilterModel) element);
            filterSettings.setActiveFilterIndex(activeFilterIndex);
        }
        filterSettings.getTableViewer().refresh();
    }

}
